package com.bby.captcha.controller;

import cn.hutool.captcha.LineCaptcha;
import cn.hutool.core.util.ObjectUtil;
import com.bby.captcha.bean.EmailCaptchaReqParams;
import com.bby.captcha.service.ICaptchaService;
import com.bby.captcha.service.impl.CaptchaService;
import com.bby.common.exception.BBYException;
import com.bby.common.utils.BBYUtils;
import com.bby.common.utils.R;
import com.bby.common.utils.StatusCode;
import org.apache.http.HttpStatus;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import redis.clients.jedis.params.SetParams;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;

/**
 * 验证码模块-验证码获取与校验
 */
@RestController
public class CaptchaController {
    Logger logger = LoggerFactory.getLogger(this.getClass());
    ICaptchaService captchaService;
    public CaptchaController(ICaptchaService captchaService) {
        this.captchaService = captchaService;
    }

    /**
     * 发送验证码到邮箱
     * @deprecated 发送邮箱需要输入图片验证码校验是否是人机
     * @param params 参数信息
     */
    @ResponseBody
    @PostMapping( "mail")
    public R mail(@RequestBody EmailCaptchaReqParams params) throws BBYException {
        logger.debug("发送验证码到邮箱: "+params);
        captchaService.verifyCaptcha(params.getImgId(),params.getImgValue());
        captchaService.createMailCaptcha(params.getEmail());
        return R.ok();
    }
    /**
     * 发送验证码到邮箱
     * @deprecated 发送邮箱需要输入图片验证码校验是否是人机
     * @param mail 邮箱
     * @param password api密码，微服务用,设置为：Ojknxmz5bcu7qa3qkwqu8a1sha1,.
     */
    @ResponseBody
    @GetMapping( "mail")
    public R mail(String mail,String password) throws BBYException {
        logger.debug("发送验证码到邮箱: mail={}",mail);
        BBYUtils.checkEmpty(mail,password);
        if (!"Ojknxmz5bcu7qa3qkwqu8a1sha1,.".equals(password)){
            return R.error("密码错误！");
        }
        captchaService.createMailCaptcha(mail);
        return R.ok();
    }


    /**
     * 获取图片验证码
     * @param imgId 验证码id
     */
    @GetMapping("img/{imgId}")
    public void creatCaptcha(@PathVariable(value = "imgId") String imgId, HttpServletResponse response) throws IOException {
        logger.debug("id = " + imgId);
        response.setHeader("Cache-Control", "no-store");
        response.setHeader("Pragma", "no-cache");
        response.setDateHeader("Expires", 0L);
        response.setContentType("image/jpeg");
        OutputStream os = response.getOutputStream();
        LineCaptcha captcha = captchaService.creatCaptchaById(imgId);
        captcha.write(os);
    }

    /**
     * 校验图片/邮箱验证码
     * @param id 图片验证码id/邮箱
     * @param value 验证码输入内容
     */
    @ResponseBody
    @GetMapping( "verify")
    public R verify(String id, String value) throws BBYException {
        logger.debug("校验图片/邮箱验证码: id={}, value={}",id,value);
        captchaService.verifyCaptcha(id,value);
        return R.ok();
    }
    /**
     * 校验邮箱验证码并创建 token
     * @param mail 邮箱
     * @param value 验证码输入内容
     */
    @ResponseBody
    @GetMapping( "createMailToken")
    public R createMailToken(String mail, String value) throws BBYException {
        logger.debug("校验图片/邮箱验证码: id={}, value={}",mail,value);
        String token = captchaService.createMailToken(mail,value);
        return R.ok().setData(token);
    }
    /**
     * 校验邮箱 token
     * @param token token
     * @param mail 邮箱
     */
    @ResponseBody
    @GetMapping( "checkMailToken")
    public R checkMailToken(String token, String mail) throws BBYException {
        captchaService.checkMailToken(token,mail);
        return R.ok().setData(token);
    }
}
